var $ = function(id){
   return document.getElementById(id);
},
giftActivityObjId = getQueryString("giftActivityObjId"),
userObjId = getQueryString("userObjId"),
clickSource = getQueryString("clickSource"),
toListType = getQueryString("type"),
vConfirm = true;

(function (w) {
    var reward = function () {
        this.Init();
        this.buttonControl = false;
        return this;
    }, rewardData = {},
        count = 0,//控制抽奖次数
        isPrize = false,
        iscroll = {},c = {};
    reward.prototype = {
        Init: function () {
            c = this;
            this.firstNum = Math.floor(Math.random()*10);//获取首个0~9的数字
            this.getNum($("left").getElementsByTagName("li"));
            this.getNum($("middle").getElementsByTagName("li"));
            this.getNum($("right").getElementsByTagName("li"));
            //$("mask").style.height = window.screen.height + "px";
            this.data = {"url":cw.serverUrl+"/rest/v1.0/lottery/getLotteryDetail/"+ userObjId +"/" + giftActivityObjId+"?clickSource="+clickSource};
            netWork(this.data,this.initData);
            iscroll = new iScroll("body", { "useTransition": true, "vScrollbar": false, "hScroll": false, "onScrollStart": function () {
                //scrollControl = true;
            }});
            this.iscroll = iscroll;
            iscroll.refresh();
        },
        initData: function(data){
            var d = eval( "(" + data + ")" );
            if(d.result.status == 1){
               $("activity_name").innerText = d.data.name;
               $("community_name").innerText = d.data.communityName;
               $("reward_desc").innerHTML = d.data.desc;
                var start = new Date(d.data.startTime),
                end = new Date(d.data.endTime);
                $("ac_time").innerHTML = resetNum(start.getMonth()+1) + "月" + resetNum(start.getDate()) + "日 " +resetNum(start.getHours()) + ":" + resetNum(start.getMinutes()) + " - " + resetNum(end.getMonth()+1) + "月" + resetNum(end.getDate()) + "日 " +resetNum(end.getHours()) + ":" + resetNum(end.getMinutes());
                iscroll.refresh();
                if(new Date < start){
                    $("start_button").innerText = "敬请期待";
                    return;
                }else if(new Date >= end){
                    $("start_button").innerText = "活动结束";
                    $("start_button").style.color = "#ccc";
                    $("start_button").className += " start-pbutton";
                    return;
                }
                count = Number(d.data.totalNum);
                if(d.data.lotteryNum < d.data.totalNum && d.data.lotteryNum > 0){
                    $("rule_title_one").innerHTML = "你还有<span>"+ d.data.lotteryNum +"次</span>抽奖机会哦";
                    count = d.data.lotteryNum;
                    if(d.data.superPrize){
                        $("super_prize").style.display = "none";
                        $("rule_title_one").style.lineHeight = "62px";
                    }
                }else if(d.data.lotteryNum <= 0){
                    if(!d.data.superPrize){
                        $("start_button").innerText = "抽奖结束";
                        $("start_button").style.color = "#ccc";
                        $("start_button").className += " start-pbutton";
                        if(d.data.isPrize){
                            $("rule_title_one").innerHTML = "手气不错，常来玩呀";
                        }else{
                            $("rule_title_one").innerHTML = "攒足人品，下次再战";
                        }
                    }
                    else{
                        $("rule_title").style.display = "none";
                        $("rule_title1").style.display = "block";
                        $("start_button").innerText = "抽奖结束";
                        $("start_button").style.color = "#ccc";
                        $("start_button").className += " start-pbutton";
                        if(d.data.isPrize){
                            $("rule_title1").innerHTML = "手气不错，常来玩呀";
                        }else{
                            $("rule_title1").innerHTML = "攒足人品，下次再战";
                        }
                    }
                    count = 0;
                }
            }
            vConfirm = d.data.isAuth;
            iscroll.refresh();
        },
        run: function(){
            $("left").style.transform ="translate3d(0,0,0)";
            $("left").style.webkitTransform ="translate3d(0,0,0)";

            $("middle").style.transform ="translate3d(0,0,0)";
            $("middle").style.webkitTransform ="translate3d(0,0,0)";

            $("right").style.transform ="translate3d(0,0,0)";
            $("right").style.webkitTransform ="translate3d(0,0,0)";
        },
        getNum: function(obj){
            for(var i = 1;i < obj.length-2;i++){
                obj[i].childNodes[0].src = cw.resUrl+"images/reward/"+Math.floor(Math.random()*10)+".png";
            }
        },
        getRewardNum: function(data){
            var d = eval( "(" + data + ")" );
            if(d.result.status == 1){
                $("start_button").className += " start-pbutton";
                $("start_button").innerText = "抽奖中";
                $("left").className = "left left1";
                $("middle").className = "middle middle1";
                $("right").className = "right right1";
                count--;
                c.run();
                $("left").getElementsByTagName("li")[0].childNodes[0].src = cw.resUrl+"images/reward/"+d.data.num.substring(0,1)+".png";
                $("middle").getElementsByTagName("li")[0].childNodes[0].src = cw.resUrl+"images/reward/"+d.data.num.substring(1,2)+".png";
                $("right").getElementsByTagName("li")[0].childNodes[0].src = cw.resUrl+"images/reward/"+d.data.num.substring(2)+".png";

                setTimeout(function(){
                    var l = $("left").getElementsByTagName("li").length;
                    $("left").getElementsByTagName("li")[l-1].childNodes[0].src = "/pages/static/mobile/images/"+d.data.num.substring(0,1)+".png";
                    $("middle").getElementsByTagName("li")[l-1].childNodes[0].src = "/pages/static/mobile/images/"+d.data.num.substring(1,2)+".png";
                    $("right").getElementsByTagName("li")[l-1].childNodes[0].src = "/pages/static/mobile/images/"+d.data.num.substring(2)+".png";
                },1000);

            }else if(d.result.status == 0){
                $("toast").innerText = d.result.message;
                $("toast").className += " toasts";
                c.buttonControl = false;
                return;
            }
            if(d.data.num == "666"){
                $("prize_add").innerText = "系统马上为您添加相应情值~";//活动结束后，66客服将联系你同城快递发放。
            }else{
                $("prize_add").innerText = "系统马上为您添加相应情值~";
            }
            rewardData = d;
        },
        evt: function(){
            var c = this;
            window.addEventListener("DOMContentLoaded",function(e){
                //e.preventDefault();
                if($("loading"))$("loading").style.display = "none";
            },false);
            //document.getElementById("refresh").addEventListener('touchend', function (e) { location.reload(); }, false);
            $("start_button").addEventListener("click",function(){
                ////vConfirm = true;
                if(!vConfirm){
                    if (toListType == 'ios') {
                        document.location.href = "66xiaoqu:Question:vAuth";
                    } else {
                        //alert("webviewhandle "+webviewhandle);
                        webviewhandle.goToVcommitSelect();//webviewhandle为webview注册对象
                    }
                    return;
                }
                if(c.buttonControl||!count){
                    if(!count) {
                        //this.innerText = "抽奖结束";
                        //$("rule_title_one").innerHTML = "你还有<span>"+ count +"次</span>抽奖机会哦";
                        //$("reward_num").innerText = count;
                    }
                    return;
                }
                c.buttonControl = true;

                c.data = {"url":"/rest/v1.0/lottery/generateLotteryTicket/"+ userObjId +"/" + giftActivityObjId+"?getId="+(new Date().getTime()),"sync":false,"thisObj": c,"prize": true};
                netWork(c.data,c.getRewardNum);
            },false);
            $("mask").addEventListener("touchmove",function(e){
                e.stopPropagation();
                e.preventDefault();
            },false);
            $("mask").addEventListener("click",function(e){ e.preventDefault();},false);
            $("mask").addEventListener("touchend",function(e){ e.preventDefault();},false);//ios双击bug
            $("reward_tip").addEventListener("touchmove",function(e){
                e.stopPropagation();
                e.preventDefault();
            },false);
            $("reward_fail").addEventListener("touchmove",function(e){
                e.stopPropagation();
                e.preventDefault();
            },false);

            $("reward_confirm").addEventListener("click",function(e){
                $("reward_tip").style.display = "none";
                $("mask").style.display = "none";
            },false);
            $("continue_reward").addEventListener("click",function(e){
                $("reward_fail").style.display = "none";
                $("mask").style.display = "none";
            },false);
            //最慢的动画结束后，触发事件
            $("right").addEventListener('webkitTransitionEnd', function(){
                if(rewardData.data&&rewardData.data.prize){
                    isPrize = true;
                }
                if(count != 0){
                    $("rule_title_one").innerHTML = "你还有<span>"+ count +"次</span>抽奖机会哦";
                    $("reward_num").innerText = count;
                }else {
                    $("start_button").innerText = "抽奖结束";
                    $("start_button").style.color = "#ccc";
                    $("start_button").className += " start-pbutton";
                    $("rule_title").style.display = "none";
                    $("rule_title1").style.display = "block";
                    if(!isPrize && $("rule_title1").innerText != "手气不错，常来玩呀"){
                        $("rule_title1").innerText = "攒足人品，下次再战";
                    }
                }

                if(rewardData.data && rewardData.data.prize &&! count){
                    //$("end_tip").innerHTML = "手气不错，常来玩呀";
                    $("prize").innerText = "中了"+rewardData.data.prize;
                    $("reward_tip").style.display = "block";
                    $("mask").style.display = "block";
                    $("start_button").innerText = "抽奖结束";
                    $("start_button").style.color = "#ccc";
                    $("start_button").className += " start-pbutton";
                    return;
                }else if(!count){
                    //$("end_tip").innerHTML = "攒足人品，下次再战";
                    $("fail_tip").innerHTML = "去查看中奖邻居";
                    $("continue_reward").innerText = "关闭";
                    $("reward_fail").style.display = "block";
                    $("mask").style.display = "block";
                    $("start_button").innerText = "抽奖结束";
                    $("start_button").style.color = "#ccc";
                    $("start_button").className += " start-pbutton";
                    return;
                }
                if(rewardData.data&&rewardData.data.prize){
                    $("prize").innerText = "中了"+rewardData.data.prize;
                    $("reward_tip").style.display = "block";
                    $("mask").style.display = "block";
                    //isPrize = true;
                }else{
                    $("reward_fail").style.display = "block";
                    $("mask").style.display = "block";
                }
                //按钮状态还原
                $("start_button").innerText = "开始抽奖";
                $("start_button").className = "start-button";
                //数字列表还原
                $("left").className = "left";
                $("left").style.transform ="translate3d(0,-97.5%,0)";
                $("left").style.webkitTransform ="translate3d(0,-97.5%,0)";
                $("middle").className = "middle";
                $("middle").style.transform ="translate3d(0,-97.5%,0)";
                $("middle").style.webkitTransform ="translate3d(0,-97.5%,0)";
                $("right").className = "right";
                $("right").style.transform ="translate3d(0,-97.5%,0)";
                $("right").style.webkitTransform ="translate3d(0,-97.5%,0)";

                c.buttonControl = false;
            }, false);

            $("to_list").addEventListener("click",function(){
                if (toListType == 'ios') {
                    //document.location = "66xiaoqu:Question:toList";
                    //document.location.href = "66xiaoqu:Question:toList";//ios接收消息
                } else {
                    //webviewhandle.goToAskQuestion();
                }
                //setTimeout(function(){
                    location.href = cw.htmlUrl+"reward/reward_list.html?giftActivityObjId="+giftActivityObjId+"&userObjId="+userObjId;
                //},200);
            },false);

            //无网络判断
            $("toast").addEventListener('webkitAnimationEnd', function(){
                this.className = "toast";
            }, false);
            iscroll.refresh();
        }
    }
    var resetNum = function(num){
        if(num < 10) num = "0"+num;
        return num;
    };
    w.reward = reward;
})(window);
//需要在common net里调用
var netControl = new reward();
netControl.evt();